#!/usr/local/bin/perl -w
use strict;
use utf8;
use lib '../lib';

use NG::Gen::App qw(
NG::Gen::AuthCgi
);

use JSON::XS;
use NG::Gen::Users::Constants;
my $t;
my $app = new NG::Sys::App();
$app->run;

sub on_prepare_loop {
	my ($app) = @_;
	$t = $app->template_tt->read("main");
}

sub on_before_process {
	my ($app,$form,$obj) = @_;
	unless($form->{user}){
		$form->redirect("/");
		return 0;
	}
	return 1;
}
sub cmd_default {
	my ($app,$form,$obj) = @_;
	$form->{data}{title} = $form->{user}->title;
	$form->{data}{login} = $form->{user}->login;
	$form->{data}{password} = $form->{user}->chat_pass;
	$form->{data}{path_to_chat} = $app->{path_to_chat};

	unless($form->{user}->validate){
		$form->{data}{run} = "validate_form";
		$form->{data}{start} = "dbg";
		$form->{data}{start_data} = "'чат запущен'";
		$form->{data}{setting} = encode_json($INIT_CHAT_SETTING);
	}else{
		$form->{data}{run} = "chat_setting";
		$form->{data}{run_data}
		= $form->{data}{setting}
		= encode_json($form->{user}->setting->chat),
	}
	$form->{data}{config} = encode_json({
		template_path => "/t",
		template_ver => time(),
		url => $app->{path_to_chat},
	});
	$form->process($t);
}
